package com.android.view.activity.user;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.android.model.entity.res.SendCodeResult;
import com.android.presenter.info.SendCodePresenter;
import com.android.presenter.info.VerifyCodePresenter;
import com.android.presenter.iview.ISendCodeView;
import com.android.presenter.iview.IVerifySmsView;
import com.android.util.EncryptPhoneUtil;
import com.android.util.ToastUtil;
import com.android.view.activity.base.Constants;
import com.android.R;
import com.android.model.entity.res.VerifyCodeResult;
import com.android.view.activity.base.ToolbarBaseActivity;

import butterknife.BindView;

/**
 * Security verification
 * Created by TenFace on 2018/4/23.
 */

public class SecurityVerificationActivity extends ToolbarBaseActivity implements ISendCodeView, IVerifySmsView {
    private static final String TAG = SecurityVerificationActivity.class.getSimpleName();
    @BindView(R.id.etSMS)
    EditText etSMS;
    @BindView(R.id.tvPhone)
    TextView tvPhone;
    @BindView(R.id.tvSend)
    TextView tvSend;
    @BindView(R.id.btnNext)
    Button btnNext;
    private String resetAccount;
    private static String intentKey = "ACCOUNT";
    private static String intentKey2 = "EX55PIN";
    private Context context;
    private SendCodePresenter sendCodePresenter;
    private VerifyCodePresenter verifyCodePresenter;
    private String ex55Pin;

    public static void actionStart(Context context, String account, String ex55Pin) {
        Intent intent = new Intent(context, SecurityVerificationActivity.class);
        intent.putExtra(intentKey, account);
        intent.putExtra(intentKey2, ex55Pin);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Constants.activityList.put(TAG, this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Constants.activityList.remove(TAG);
    }

    @Override
    protected void setContentLayout() {
        setContentView(R.layout.activity_security_verification);
    }

    @Override
    protected void initView() {
        context = this;
        int color = getResourceColor(R.color.white);
        setStatusBarColor(color, 0);
        setStatusBarMode(true);
        Intent intent = getIntent();
        resetAccount = intent.getStringExtra(intentKey);
        ex55Pin = intent.getStringExtra(intentKey2);
        tvPhone.setText(EncryptPhoneUtil.EncryptPhoneUtil(resetAccount) + ":");
    }

    @Override
    protected void obtainData() {
        sendCodePresenter = new SendCodePresenter(this);
        verifyCodePresenter = new VerifyCodePresenter(this);
    }

    @Override
    protected void initEvent() {
        btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (etSMS.getText().length() < 6) {
                    ToastUtil.show("请输入正确的验证码！");
                } else {
                    verifyCodePresenter.verifySmsCode(ex55Pin, resetAccount, etSMS.getText().toString(), "PHONE", "RESET_PASSWORD");
                }
            }
        });
        tvSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                restartTimer(tvSend);
                sendCodePresenter.sendSmsCode(ex55Pin, resetAccount, "PHONE", "RESET_PASSWORD");
            }
        });
    }

    public void restartTimer(View v) {
        tvSend.setClickable(false);
        codeTimer.start();
    }

    private CountDownTimer codeTimer = new CountDownTimer(60000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            tvSend.setText((millisUntilFinished / 1000) + "s");
        }

        @Override
        public void onFinish() {
            try {
                tvSend.setText(getResources().getString(R.string.register_send));
                tvSend.setClickable(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };

    @Override
    public void sendCodeSuccess(SendCodeResult sendCodeResult) {

    }

    @Override
    public void sendCodeFail(int status, String desc) {

    }

    @Override
    public void verifySmsSuccess(VerifyCodeResult verifyCodeResult) {
        if (verifyCodeResult != null) {
            SetNewPasswordActivity.actionStart(context, verifyCodeResult.getToken());
        } else {
        }
    }

    @Override
    public void verifySmsFail(int status, String desc) {

    }
}
